package com.manfred.uddi.test;

import java.util.Vector;

import org.uddi4j.client.UDDIProxy;
import org.uddi4j.datatype.binding.BindingTemplate;
import org.uddi4j.datatype.binding.InstanceDetails;
import org.uddi4j.datatype.binding.TModelInstanceDetails;
import org.uddi4j.datatype.binding.TModelInstanceInfo;
import org.uddi4j.datatype.service.BusinessService;
import org.uddi4j.datatype.tmodel.TModel;
import org.uddi4j.response.AuthToken;
import org.uddi4j.response.ServiceDetail;
import org.uddi4j.response.ServiceInfo;
import org.uddi4j.response.ServiceList;
import org.uddi4j.transport.TransportFactory;
import org.uddi4j.util.CategoryBag;
import org.uddi4j.util.KeyedReference;

public class FindTypeOfService {

	public static void main(String args[]) {
		// String inquiryURL = "http://localhost:8080/juddi/inquiry";
		// String publishURL = "http://localhost:8080/juddi/publish";
		String inquiryURL = "http://localhost:8080/juddiv3/services/inquiryv2";
		String publishURL = "http://localhost:8080/juddiv3/services/publishv2";

		String userId = "juddi";
		String credential = "juddi";

		String transportClass = "org.uddi4j.transport.ApacheAxisTransport";
		System.setProperty(TransportFactory.PROPERTY_NAME, transportClass);

		UDDIProxy proxy = new UDDIProxy();

		try {

			proxy.setInquiryURL(inquiryURL);
			proxy.setPublishURL(publishURL);

			AuthToken token = proxy.get_authToken(userId, credential);
			System.out.println("Security authToken:"
					+ token.getAuthInfoString());
			KeyedReference wsdlNotation = new KeyedReference(
					TModel.TYPES_TMODEL_KEY, "wsdlSpec",
					"C1ACF26D-9672-4404-9D70-39B756E62AB4");
			KeyedReference typeNotation = new KeyedReference(
					"ntis-gov:naics:1997", "511110",
					"C0B9FE13-179F-413D-8A5B-5004DB8E5BB2");
			CategoryBag catBag = new CategoryBag();
			catBag.add(wsdlNotation);
			catBag.add(typeNotation);

			ServiceList serviceList = proxy.find_service(null, null, catBag,
					null, null, 5);

			Vector serviceInfoVector = serviceList.getServiceInfos()
					.getServiceInfoVector();
			for (int i = 0; i < serviceInfoVector.size(); i++) {
				ServiceInfo serviceInfo = (ServiceInfo) serviceInfoVector
						.elementAt(i);

				System.out.println("Service name: "
						+ serviceInfo.getDefaultNameString());
				System.out.println("Service key: "
						+ serviceInfo.getServiceKey());

				ServiceDetail serviceDetail = proxy
						.get_serviceDetail(serviceInfo.getServiceKey());

				BusinessService thisService = (BusinessService) serviceDetail
						.getBusinessServiceVector().elementAt(0);
				if (thisService.getBindingTemplates().size() > 0) {
					BindingTemplate thisBinding = (BindingTemplate) thisService
							.getBindingTemplates().get(0);

					TModelInstanceDetails tModelDetails = thisBinding
							.getTModelInstanceDetails();
					TModelInstanceInfo tModelInfo = tModelDetails.get(0);
					InstanceDetails instanceDetails = tModelInfo
							.getInstanceDetails();
					String wsdlDocument = instanceDetails.getOverviewDoc()
							.getOverviewURLString();
					System.out.println("Document located at " + wsdlDocument);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}